const app = getApp();
var _hideC = { 1: 'hide', 2: 'hide', 3: 'hide', 4: 'hide', 5: 'hide'};
Page({
  data: {
    cndUrl: app.domainName,
    p0Ti:{'b':'hide',0: '', 1: 'hide', 2: 'hide', 3: 'hide', 4: 'hide', 5: 'hide', 6: 'hide', 7: 'hide', 8: 'hide', 9: 'hide', 10: 'hide', 11: 'hide', 12: 'hide',13:'hide',14:'hide'},
    poX:{ 1: 'hide', 2: 'hide', 3: 'hide', 4: 'hide', 5: 'hide',6: 'hide', 7: 'hide', 8: 'hide', 9: 'hide', 10: 'hide', 11: 'hide', 12: 'hide',13:'',14:''},
    poXId:{ 1: '1', 2: '2', 3: '3', 4: '1', 5: '1',6: '1', 7: '1', 8: '1', 9: '1', 10: '1', 11: '1', 12: '1',13:'',14:''},
    poXTu:{1:'1',2:'2',3:'3'},
    poUser:{1:0,2:0,3:0},
    p0Lock:'',
    hideId:0,
    posTop:0,
    p0Id:1, 
    pId:0,
    p1C:{1:'fadeIn animate3',2:'',3:''},
    p2C:_hideC,
    p3C:_hideC,
    p4C:_hideC,
    p5C: _hideC,
    hideP:{0:'hide', 1:'',2:'hide',3:'hide',4:'hide',5:'hide',6:'hide'}, //页面隐藏与否
    showMenu:false,
    indicatorDots: false,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    current:0,
    showLx1: true,
    q1float: false,
    p1nclick:'hide', //下一页按钮隐藏 
    q1savepic:"1",
    showLx2: false,
    q2float: false,
    picShare:{0:false,1:false,2:'',3:''},
    cleft1:-1000,
    showLx4: false,
    q4float: false,
    cleft4: -1000,
    showLx5: false,
    q5float: false,
    showLx6: false,
    q6float: false,
    showend: false,
    fcenTop:0,
    showfloatCen1:false,
    cshowBtn: false,
    stbFadeOut:'',
  },
  onLoad: function (options) {
      var _this=this;
      setTimeout(function(){
        _this.setData({p1C:{1:''} });
      },1000);
    //练习二 随机一张图片
    var randomNum = Math.floor(Math.random() * 10);
    console.log(randomNum)
    this.setData({
      q1savepic: 1
    })
 
     //app.globalData.openid
  },
  gotolx1(){
    var _this = this;
    var query = wx.createSelectorQuery();
    _this.setData({stbFadeOut:'anid8 fadeOut'})
    query.select('.startBtn').boundingClientRect(function (rect) {
      console.log(rect)
      _this.setData({
        fcenTop:rect.top,
        q1float:true
      })

      setTimeout(function(){
        _this.setData({
          showfloatCen1:true,
          showLx1: true
        })
      },1000)

    }).exec();
  },
  saveImagehb(){
    this.selectComponent('#huabu1').saveImghb();
  },
  gotoLx2(){
    var _this = this;
    _this.setData({
      showLx2: true,
      q2float: true,
    })
    setTimeout(function(){
      _this.setData({
        showLx1: false,
        q1float: false,
      })
    },500)
  },
  goNextLx(e){
    var _this = this;
     var _id=e.currentTarget.dataset.id;
     if(2 == _id){
      _this.setData({
        showLx4: true,
        q4float: true,
        cleft4: -1000,
      })
      setTimeout(function(){
        _this.setData({
          showLx2: false,
          q2float: false,
          cleft1:-1000,
        })
      },500)
     }
     if(4 == _id){
      _this.setData({
         showLx5: true,
         q5float: true,
      })
      setTimeout(function(){
        _this.setData({
          showLx4: false,
          q4float: false,
          cleft4: -1000,
        })
      },500)
     }
     if(5 == _id){
      _this.setData({
        showLx6: true,
        q6float: true,
      })
      setTimeout(function(){
        _this.setData({
          showLx5: false,
          q5float: false,
        })
      },500)
     }

     if(6 == _id){
      _this.setData({
        showend: true
      });
      setTimeout(function(){
        _this.setData({
          showLx6: false,
          q6float: false,
        })
      },500);
     }

  },
  closeQfloat(e){
     var _id=e.currentTarget.dataset.id;
     if('pic' == _id){
      var _picShare=this.data.picShare;
      _picShare[1]=false;
      _picShare[0]=false;
      this.setData({picshare:_picShare,cleft1:0,cleft4:0})
         return;
     }
  },
  closeQ1float(){
    this.setData({
      q1float:false
    })
  },
  closeQ2float() {
    this.setData({
      q2float: false,
      cleft1:0
    })
  },
  closeQ4float() {
    this.setData({
      q4float: false,
      cleft4: 0,
      cshowBtn: true
    })
  },
  closeQsfloat(e){
   var _id=e.currentTarget.dataset.id;
   if(5 == _id){
    this.setData({q5float: false })
   }
   if(6 == _id){
      this.setData({q6float: false })
   }
 },
  changep4fn(e){
    console.log(e.detail.current)
    this.setData({
      current: e.detail.current
    })
  },
 
  menuBtn(){
    var showMenu = this.data.showMenu;
    var _this = this;
    console.log(showMenu)
    if (!showMenu){
      this.setData({
        showMenu:true

      })

      if (_this.data.showLx2) {
        this.setData({
          cleft1: -1000,
          cshowBtn: false
        })
        this.selectComponent("#huabu1").closeColorboard()
      }

      if (_this.data.showLx4) {
        this.setData({
          cleft4: -1000,
          cshowBtn: false
        })
        this.selectComponent("#huabu2").closeColorboard()
        this.selectComponent("#huabu2").createImg()
      }

    }else{
      this.setData({
        showMenu: false
      })

      if (_this.data.showLx2) {
        this.setData({
          cleft1: 0,
          cshowBtn: true
        })
      }

      if (_this.data.showLx4) {
        this.setData({
          cleft4: 0,
          cshowBtn: true
        })
      }

    }
  },
  preventDefaultfn(e){
    console.log(123)
  },
  goNext: function (e) {
     var _this=this;
     var _id=this.data.pId;
     var _p1nclick=this.data.p1nclick;
     var _hideP=this.data.hideP;
     if('hide' == _p1nclick){return;}
     this.setData({p1nclick:'hide'});
     setTimeout(function(){_this.setData({ p1nclick:''});}, 1200);
     if(0 == _id){
      _hideP[0]='hide';
      _hideP[1]='animate3 fadeIn';
      this.setData({ pId:1, hideP:_hideP});
      return;
     }
     if(1 == _id){
       _hideP[2] = '';
       this.setData({ hideP:_hideP});
       this.go1();
     }
     if(2 == _id){
      _hideP[3] = '';
       this.setData({ hideP:_hideP}); 
      this.go2();
    }
    if (3 == _id) {
      _hideP[4] = '';
      this.setData({ hideP: _hideP });
      this.go3();
    }
    if (4 == _id) {
      _hideP[5] = '';
      this.setData({ hideP: _hideP });
      this.go4();
    }
  },
  go4: function (e) {
    var _this = this;

    var _next = { 1: 'animate2 fadeIn', 2: 'hide', 3: 'hide', 4: 'hide' };
    var _prev = { 1: 'anid8 fadeOut', 2: 'anid5 fadeOutLeft', 3: 'anid5 fadeOutLeft', 4:'anid5 fadeOutLeft'};
    _this.setData({
      p4C: _prev,
      p5C: _next,
      pId: 5
    });
    setTimeout(function () {
      _next[2] = 'anid8 fadeInRight';
      _next[3] = 'anid8 fadeInRight';
      _next[4] = 'fadeIn animate3 dy1';
      _this.setData({
        p5C: _next
      });
    }, 400);

  },
  go3: function (e) {
    var _this = this;

    var _next = { 1: 'animate2 fadeIn', 2: 'hide', 3: 'hide',4:'hide'};
    var _prev = { 1: 'anid8 fadeOut', 2: 'anid5 fadeOutLeft', 3: 'anid5 fadeOutLeft', 4:'anid5 fadeOutLeft'};
    _this.setData({
      p3C: _prev,
      p4C: _next,
      pId: 4
    });
    setTimeout(function () {
      _next[2] = 'anid8 fadeInRight';
      _next[3] = 'anid8 fadeInRight';
      _next[4] = 'fadeIn animate3 dy1';
      _this.setData({
        p4C: _next
      });
    }, 400);

  },
  go2: function (e) {
    var _this=this;
    var _next={1:'animate2 fadeIn', 2:'hide',3:'hide',4:'hide'};
    var _prev={1:'anid8 fadeOut',2:'anid5 fadeOutLeft',3:'anid5 fadeOutLeft',4:'anid5 fadeOutLeft'};
   _this.setData({
     p2C:_prev,
     p3C:_next,
     pId:3
   });
   setTimeout(function(){
     _next[2] = 'anid8 fadeInRight';
     _next[3] = 'anid8 fadeInRight';
     _next[4] = 'fadeIn animate3 dy1';
     _this.setData({
        p3C:_next
     });
   },400); 
 
  },
  go1: function (e) {
    var _this=this;
    var _next={1:'animate2 fadeIn', 2:'hide',3:'hide',4:'hide'};
    var _prev={1:'anid8 fadeOut',2:'anid5 fadeOutLeft',3:'anid5 fadeOutLeft'};
   _this.setData({
     p1C:_prev,
     p2C:_next,
     pId:2
   });
   setTimeout(function(){
    _next[2] = 'anid8 fadeInRight';
    _next[3] = 'anid8 fadeInRight';
    _next[4] = 'fadeIn animate3 dy1';
     _this.setData({
         p2C:_next
     });
   },400); 
  },
  saveImg(_data) {
    wx.request({
      url: app.domainName + 'web/?c=pi2',
      method: 'POST',
      data: {op:app.globalData.openid,data:_data }, header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) { 
        wx.showLoading({
          title: '保存中...',
        })
        wx.downloadFile({url: app.domainName+'upic/'+app.globalData.openid+'-u2.jpg?v='+Math.random(),  
          success (res) {
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            });
            setTimeout(function(){
              wx.saveImageToPhotosAlbum({
                filePath:res.tempFilePath,
                success(res1) {   
                }  })
            }, 400);
            
          }
        })
      }
    });
  },
  lx4Event(res){
    var _picShare=this.data.picShare;
    _picShare[2]=res.detail;
    this.setData({picshare:_picShare,cleft4:-1000,})
    this.saveImg(res.detail);
  },
  lx2Event(res) {
 
    var _picShare=this.data.picShare;
    _picShare[2]=res.detail;
    this.setData({picshare:_picShare,cleft1:-1000,})
    this.saveImg(res.detail);
     
  },
  getBase641() { // 练习二 获取图片 base64
    var _picShare=this.data.picShare;
      _picShare[1]=true;
    this.setData({picshare:_picShare})
    this.selectComponent("#huabu1").getBase64()
  },
  getBase642() { // 练习四 获取图片 base64
    var _picShare=this.data.picShare;
      _picShare[1]=true;
      this.setData({picshare:_picShare});
    this.selectComponent("#huabu2").getBase64()
  },
  saveImagelx4() { // 练习四 保存图片
    this.selectComponent("#huabu2").saveImagelx2()
  },
  saveImagelx2() { // 练习二 保存图片
    // var _this = this;
    // this.saveImage(function () {
    //   _this.loadCanvasImg("chuabu1")
    // })
    this.selectComponent("#huabu1").saveImagelx2()
  },
  saveImagelx1(){ // 练习一 保存图片
    var _this = this;
    var _picShare=this.data.picShare;
    _picShare[0]=true;
    _picShare[3]= app.domainName+'img/q1/1.jpg';
    this.setData({picshare:_picShare});


    this.saveImage(function(){
      _this.loadimg()
    })
  },
  saveImage(fn) { // 打开保存图片授权
    var _this = this;
    console.log(123)
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: () => {
              // 同意授权
              console.log("同意授权")
              if (fn) {
                fn()
              }
            },
            fail: (res) => {
              console.log(res);
            }
          })
        } else {
          // 已经授权了
          console.log("已经授权了");
         if(fn){
           fn()
         }
        }
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },
  loadimg() { // 保存图片到相册
    var _this = this;
    wx.showLoading({
      title: '保存中...',
    })
    var imgurl = this.data.q1savepic;
    imgurl=1;
    wx.downloadFile({
      url: app.domainName+'img/q1/'+imgurl+'.jpg',　　　　　　　//需要下载的图片url
      success: function (res) {　　
        console.log(res);
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            });
          },
          complete(){
            wx.hideLoading();
          }
        });

      }
    })
  },
  openrule(e){ 
    var _id=e.currentTarget.dataset.id;
    if(1 == _id){
      this.setData({q1float:true});return;
    }
    if(2 == _id){
      this.setData({q2float:true, cleft1:-1000});return;
    }
    if(3 == _id){
      this.setData({q3float:true});return;
    }
    if(4 == _id){
      this.setData({q4float:true, cleft4:-1000});return;
    }
    if(5 == _id){
      this.setData({q5float:true});return;
    }
    if(6 == _id){
      this.setData({q6float:true});return;
    }
  },
  menuClick(e){ 
    var _this = this;
    var _id=e.currentTarget.dataset.id;
    var _hideP={1:'hide',2:'hide',3:'hide',4:'hide',5:'hide',6:'hide'};
    var _next = { 1: 'animate2 fadeIn', 2: 'animate2 fadeIn', 3: 'animate2 fadeIn',4:'animate2 fadeIn'};
    _hideP[_id] = '';
    _this.setData({ hideP:_hideP, pId:_id});
    if(1 == _id){
        _this.setData({p1C:_next});
    }
    if(2 == _id){
      _this.setData({p2C:_next});
    }
    if(3 == _id){
      _this.setData({p3C:_next});
    }
    if(4 == _id){
      _this.setData({p4C:_next});
    }
    if(5 == _id){
      _this.setData({p5C:_next});
    }
     console.log(_id);
 
  },
  p0start(e){
      var _p0Ti=this.data.p0Ti;
      _p0Ti[0]='hide';
      _p0Ti['b']='';
      _p0Ti[1]='';
      this.setData({p0Ti:_p0Ti});
  },
  poxuan(e){
    var _this=this;
    var _p0Lock=this.data.p0Lock;
    if('lock' == _p0Lock){return;}
    this.setData({p0Lock:'lock'});
    var _id=e.currentTarget.dataset.id;
    var _poX=this.data.poX;
    var _poXTu={1:'1',2:'2',3:'3'};
    var _p0Id=this.data.p0Id;
    var _p0Ti=this.data.p0Ti;
    var _poXId=this.data.poXId;
    var _poUser=this.data.poUser;
    if(1 == _id){
      _poXTu[1]='1-2';
      _poUser[1]=_poUser[1]+1;
    }
    if(2 == _id){
      _poXTu[2]='2-2';
      _poUser[2]=_poUser[2]+1;
    }
    if(3 == _id){
      _poXTu[3]='3-2';
      _poUser[3]=_poUser[3]+1;
    }
    _poXId[_p0Id]=_id;
     _poX[_p0Id]='';
     if(12 == _p0Id){
        if(_poUser[3] > 2){
          _p0Id=13;
        }else{
          _p0Id=14;
        }
        _p0Ti['b']='';
     }else{
      _p0Id++;
     }
   
    _p0Ti[_p0Id]='';
    console.log('s:'+_p0Id);  
    this.setData({poXTu:_poXTu, poX:_poX, p0Id:_p0Id,poXId:_poXId,poUser:_poUser});
    if(4 < _p0Id){
      _this.doScroll();
    }
    setTimeout(function(){
       _this.setData({p0Ti:_p0Ti });
       console.log('time:'+_p0Id);
       if(3 < _p0Id){
        _this.doScroll();
       }
    }, 1200);
    setTimeout(function(){
      if(_p0Id == 13 || 14 == _p0Id){
      _p0Ti['b']='animated fadeOut';  
        _this.setData({ p0Ti:_p0Ti, p1nclick:'animate3 fadeIn'});
        setTimeout(function(){_this.setData({p0Lock:'' });}, 3000);
        return;
      }
      _poXTu={1:'1',2:'2',3:'3'};
       _this.setData({poXTu:_poXTu,p0Lock:'' });
    }, 2200);
     
},
doScroll(){
 var _this=this;
  setTimeout(function(){ 
    var _posTop=_this.data.posTop;
      _posTop=_posTop+200;
    _this.setData({posTop:_posTop}); 
  }, 200);
},
onShareAppMessage: function (e) {
    var _imgUrl='';
    if (e.from === 'button') {
      if('lx2' == e.target.dataset.id){
          _imgUrl=app.domainName+'upic/'+app.globalData.openid+'-2.png?v='+Math.random();
      }
      if('lx1' == e.target.dataset.id){
        _imgUrl=app.domainName+'img/q1/'+this.data.q1savepic+'.jpg';
       }
      }
      return {
        title: '保时捷艺术疗愈',
        imageUrl:_imgUrl,
        path: '/pages/index/index'
      }
  
     
}
})